package com.ricky.onlineShop.shop.trading.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ricky.admin.util.LoginUserUtils;
import com.ricky.base.repository.BaseRepository;
import com.ricky.common.core.domain.R;
import com.ricky.common.exception.TradingException;
import com.ricky.onlineShop.shop.trading.pojo.TradingPerformance;
import com.ricky.onlineShop.shop.trading.repository.TradingPerformanceRepository;
import com.ricky.onlineShop.shop.trading.service.ITradingPerformanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author staticlan
 * @Date 2024/7/17 16:56
 * @Description:
 */
@Service
@Slf4j
public class TradingPerformanceServiceImpl implements ITradingPerformanceService {
    @Autowired
    private TradingPerformanceRepository tradingPerformanceRepository;
    @Override
    public BaseRepository<TradingPerformance, String> getRepository() {
        return tradingPerformanceRepository;
    }


    @Override
    public Map<String, Object> myPerformance() {
        String userId = LoginUserUtils.getUserId();
        if (StrUtil.isNotEmpty(userId)) {
            Map<String, Object> map = new HashMap<>();
            map.put("performance", 0);
            map.put("royalty", 0);
            map.put("allPerformance", 0);
            List<TradingPerformance> byEmployeeId = tradingPerformanceRepository.findByEmployeeId(userId);
            if (ObjectUtil.isNotEmpty(byEmployeeId)) {
                BigDecimal performance = BigDecimal.ZERO;
                BigDecimal royalty = BigDecimal.ZERO;
                for (TradingPerformance tradingPerformance : byEmployeeId){
                    performance = performance.add(tradingPerformance.getPerformance());
                    royalty = royalty.add(tradingPerformance.getRoyalty());
                }
                map.put("performance", performance);
                map.put("royalty", royalty);
            }
            // 首页查询总绩效
            List<TradingPerformance> all = tradingPerformanceRepository.findAll();
            // 通过网店id去重
            List<TradingPerformance> collect = new ArrayList<>(all.stream()
                    .collect(Collectors.toMap(
                            TradingPerformance::getTradingId, // 映射的键
                            Function.identity(), // 映射的值
                            (oldValue, newValue) -> oldValue // 解决键冲突的策略：保留旧值
                    ))
                    .values()); // 收集为列表
            if (ObjectUtil.isNotEmpty(collect)) {
                BigDecimal allPerformance = BigDecimal.ZERO;
                for (TradingPerformance tradingPerformance : collect){
                    allPerformance = allPerformance.add(tradingPerformance.getPerformance());
                }
                map.put("allPerformance", allPerformance);
            }
            return map;
        }else {
            throw new RuntimeException("当前登陆人有误！");
        }
    }

    @Override
    public List<TradingPerformance> findByTradingId(String tradingId) {
        return tradingPerformanceRepository.findByTradingId(tradingId);
    }
}
